package com.longge;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class MissingInteger {


    /**
     * 如果一个前缀 nums[0..i] 满足对于 1 <= j <= i 的所有元素都有 nums[j] = nums[j - 1] + 1 ，那么我们称这个前缀是一个 顺序前缀 。
     *
     * @param nums
     * @return
     */
    public int missingInteger(int[] nums) {

        Set<Integer> numsSet = IntStream.of(nums).boxed().collect(Collectors.toSet());

        int sum = nums[0], maxSum = nums[0], maxCount = 1, curCount = 1;

        for (int i = 1; i < nums.length; i++) {
            if ((nums[i] == nums[i - 1] + 1) ) {
                sum += nums[i];
                curCount++;
                if (maxCount < curCount) {
                    maxCount = curCount;
                    maxSum = sum;
                }
            } else {
                break;
            }
        }
        int res = maxSum;
        while (numsSet.contains(res)) {
            res++;
        }
        return res;

    }

    public static void main(String[] args) {
        int i = new MissingInteger().missingInteger(new int[]{4, 5, 6, 7, 8, 8, 9, 4, 3, 2, 7});
        System.out.println(i);
    }
}
